﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using DeviceInfoManage.BusinessLogicLayer;
using System.Data;

public partial class UserManage : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
    string _cbMessage = "";
    public static string preName = "";
    public void RaiseCallbackEvent(String eventArgument)
    {
        try
        {
            string[] cmd = eventArgument.Split(',');
            string userid = cmd[1];



            if (cmd[0] == "delete") //删除
            {
                //foreach (DataRow dr in (Session["adminUsers"] as DataRowCollection))
                //{
                //    if (dr["adminUserName"].ToString() == userid)
                //    {
                (new Admin()).Delete(userid);
                //    }
                //}
                Page.ClientScript.ValidateEvent("", this.ToString());
                _cbMessage = "delete," + userid;
            }
            else  //需要验证用户名的合法性
            {
                Regex r = new Regex("^[a-zA-Z_]\\w*$");
                if (r.IsMatch(cmd[2]) == false)
                {
                    _cbMessage = "用户名必须由大小写字母、数字，和下划线“_”组成，且第一位为大小写字母";
                    return;
                }
                if (cmd[3].Trim() == "")
                {
                    _cbMessage = "密码不能为空";
                    return;
                }
            }

            if (cmd[0] == "save") //保存
            {
                Admin admin = new Admin();
                admin.AdminUserName = cmd[2];
                admin.AdminPassword = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(cmd[3], "MD5").ToString();
                admin.AdminType = cmd[4];
                admin.UserName = cmd[5];

                if (cmd.Length < 6) return;

                if (userid != "")
                {

                    foreach (DataRow dr in (Session["adminUsers"] as DataRowCollection))
                    {
                        if (dr["adminUserName"].ToString() == userid)
                        {
                            (new Admin()).Update(admin);
                        }
                    }
                }


                Page.ClientScript.ValidateEvent("", this.ToString());
                _cbMessage = "save," + userid + "," + cmd[2] + "," + admin.AdminPassword + "," + cmd[4] + "," + cmd[5];
            }

            if (cmd[0] == "add") //新增
            {
                if (cmd.Length < 6) return;
                userid = cmd[2];
                //if ()
                //{
                //    _cbMessage = "已存在该用户";
                //    return;
                //}
                Admin admin = new Admin();
                admin.AdminUserName = cmd[2];
                admin.AdminPassword = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(cmd[3], "MD5").ToString();
                admin.AdminType = cmd[4];
                admin.UserName = cmd[5];
                (new Admin()).Add(admin);
                Page.ClientScript.ValidateEvent("", this.ToString());
                _cbMessage = "add,," + cmd[2] + "," + admin.AdminPassword + "," + cmd[4] + "," + cmd[5];
            }
        }
        catch (Exception ex)
        {
            _cbMessage = ex.Message;
        }

    }


    public string GetCallbackResult()
    {
        return _cbMessage;
    }

    protected override void Render(HtmlTextWriter writer)
    {
        Page.ClientScript.RegisterForEventValidation("", this.ToString());
        base.Render(writer);
    }


    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            ClientScriptManager cs = Page.ClientScript;
            String cbReference = cs.GetCallbackEventReference("'" +
                Page.UniqueID + "'", "arg", "ReceiveServerData", "",
                "ProcessCallBackError", false);
            String callbackScript = "function CallTheServer(arg, context) {" +
                cbReference + "; }";
            cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer",
                callbackScript, true);

            Load_Data();
            if (Session["adminType"].ToString() != "管理员")
            {
                btnAdd.Attributes.Add("disabled", "disabled");
                btnDel.Attributes.Add("disabled", "disabled");
                btnSave.Attributes.Add("disabled", "disabled");
            }
        }
    }

    private void Load_Data()
    {
        //preName = MainWindowHolder.ClientID.Replace("MainWindowHolder", "");
        preName = tbUser.ClientID.Replace("tbUser", ""); //模板页面增加的前缀
        tbUser.Rows.Clear();
        tbUser.Width = Unit.Parse("90%");
        tbUser.CellPadding = 0;
        tbUser.CellSpacing = 3;
        tbUser.HorizontalAlign = HorizontalAlign.Center;

        TableRow r = new TableRow();
        TableCell c = new TableCell();
        r.ID = "head";
        c.VerticalAlign = VerticalAlign.Middle;
        c.HorizontalAlign = HorizontalAlign.Center;
        c.Width = Unit.Parse("3%");
        c.Height = 20;
        //c.Font.Name = "宋体";
        c.Text = "  ";
        r.Cells.Add(c);

        c = new TableCell();
        c.VerticalAlign = VerticalAlign.Middle;
        c.HorizontalAlign = HorizontalAlign.Left;
        c.Width = Unit.Parse("30%");
        c.Height = 20;
        //c.Font.Name = "宋体";
        c.Text = "账号名";
        r.Cells.Add(c);

        c = new TableCell();
        c.VerticalAlign = VerticalAlign.Middle;
        c.HorizontalAlign = HorizontalAlign.Left;
        c.Width = Unit.Parse("60%");
        c.Height = 20;
        //c.Font.Name = "宋体";
        c.Text = "类型";
        r.Cells.Add(c);
        tbUser.Rows.Add(r);

        DataSet ds = (new Admin()).GetUsers();
        if (ds.Tables[0].Rows.Count > 0)
        {
            Session["adminUsers"] = ds.Tables[0].Rows;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                string logid = dr["adminUserName"].ToString();
                r = new TableRow();
                r.ID = logid;
                c = new TableCell();
                c.VerticalAlign = VerticalAlign.Middle;
                c.HorizontalAlign = HorizontalAlign.Center;
                c.Width = Unit.Parse("3%");
                c.Height = 20;
                //c.Font.Name = "宋体";
                c.Text = "·";
                r.Cells.Add(c);
                c = new TableCell();
                c.VerticalAlign = VerticalAlign.Middle;
                c.Width = Unit.Parse("30%");

                c.ID = dr["adminUserName"].ToString() + "1";
                c.Text = "<a href=\"#\" onClick='displaydetail(\"" + dr["adminUserName"].ToString() + "\",\"" + dr["adminPassword"].ToString() + "\",\"" + dr["adminType"].ToString() + "\",\"" + dr["UserName"].ToString() + "\")'>" + logid + "</a>&nbsp;";
                r.Cells.Add(c);

                c = new TableCell();
                c.ID = dr["adminUserName"].ToString() + "2";
                c.VerticalAlign = VerticalAlign.Middle;
                c.Width = Unit.Parse("60%");
                c.Text = "<a href=\"#\" onClick='displaydetail(\"" + dr["adminUserName"].ToString() + "\",\"" + dr["adminPassword"].ToString() + "\",\"" + dr["adminType"].ToString() + "\",\"" + dr["UserName"].ToString() + "\")'>" + dr["adminType"].ToString() + "</a>&nbsp;";
                r.Cells.Add(c);

                tbUser.Rows.Add(r);
            }
        }
    }

}
